<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Comment_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 返回所有评价的数量
	 */
	public function count_comments(){
		$query = $this->db->get('t_comment');
		return $query->num_rows();
	}
	/*
	 * 根据分页信息得到评价
	 */
	public function get_comments($page,$offset){
		$this->db->select('t_comment.id,t_product.name,t_comment.score,t_comment.content,t_comment.date,t_user.pname,t_comment.reply');
		$this->db->from('t_comment');
		$this->db->join('t_product', 't_comment.productid = t_product.id','left');
		$this->db->join('t_user', 't_comment.userid = t_user.id','left');
		$this->db->limit($page, $offset);
		$this->db->order_by("id", "desc");
		$query = $this->db->get();
		return $query->result_array();
	}
	/*
	 * 根据ID得到评价信息
	 */
	public function load($id){
		$query = $this->db->get_where('t_comment',array('id' => $id));
		if ($row = $query->row_array()){
			return $row;
		}
		return array();
	}
	/*
	 *更新评价信息
	 */
	public function update($id,$newcomment){
		$this->db->where('id', $id);
		$this->db->update('t_comment', $newcomment);
		return $this->db->affected_rows()>0;
	}
	/*
	 * 删除评价信息
	 */
	public function delete($id){
		$this->db->where('id', $id);
		$this->db->delete('t_comment');
		return $this->db->affected_rows()>0;
	}
}